<template>
    <div>
        <el-dialog
                :before-close="closeDialog"
                :title="isPass?'批量通过':'批量退回'"
                :visible.sync="localFormVisible" width="50%"
        >
            <el-table
                :data="reviewTable">
                <el-table-column label="序号" type="index" ></el-table-column>
                <el-table-column label="申请员工" prop="empName"></el-table-column>
                <el-table-column label="部门名称" prop="deptName"></el-table-column>
                <el-table-column label="订餐人员" prop="members"></el-table-column>
                <el-table-column label="总金额" prop="totalprice" :formatter="formatPrice"></el-table-column>
                <el-table-column label="订餐备注" prop="orderNote"></el-table-column>
                <el-table-column
                        label="审核意见"
                        width="180"
                >
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.reviewNote" class="item__input" placeholder="请输入内容"></el-input>
                    </template>
                </el-table-column>
            </el-table>
            <div slot="footer" class="dialog-footer">
                <el-button @click="closeDialog">取消</el-button>
                <el-button type="primary" @click="submitReview">保存</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: "BatchReview",
    data() {
        return {
            localFormVisible: this.formVisible,
        }
    },
    methods: {
        closeDialog() {
            this.localFormVisible = false;
            //通知父组件修改formVisible的值为false
            this.$emit('update:formVisible', false)
        },
        formatPrice(row, column, cellValue, index) {
            // 使用 toFixed 方法来格式化小数，保留两位小数
            // 注意：toFixed 返回的是一个字符串
            return cellValue.toFixed(2);
        },
        submitReview() {
            // 检查每一行的审核意见是否都已填写
            for (let i = 0; i < this.reviewTable.length; i++) {
                if (!this.reviewTable[i].reviewNote) {
                    this.$message({
                        type: 'error',
                        message: '请为所有所有订餐填写审核意见'
                    });
                    return; // 如果有未填写的，直接返回
                }
            }
            let reviewNotes = []
            for (let i = 0; i < this.reviewTable.length; i++) {
                reviewNotes.push(
                    {
                        "orderId":this.reviewTable[i].orderId,
                        "reviewNote":this.reviewTable[i].reviewNote
                    }
                )
            }
            this.$emit('submitReview', reviewNotes)
        }
    },
    props: {
        isPass: {
            type: Boolean,
            default: true
        },
        reviewTable: {
            type: Array
        },
        formVisible: {
            type: Boolean,
            default: false
        },
    },
    watch: {
        formVisible(newVal) {
            this.localFormVisible = newVal; // 当 `prop` 改变时，更新本地变量
            if (newVal) {
                if (this.$refs['formRef']) {
                    this.$refs['formRef'].resetFields(); // 当弹框打开时重置表单
                }
            }
        },

    },
}
</script>

<style scoped>

</style>